package com.nidu.demo.dict.ability;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.dict.gateway.DictDataGateway;
import com.nidu.demo.dict.model.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DictDataDomainService {

    private final DictDataGateway gateway;

    public void validateDuplicate(String dictType, String value, Long excludeId) {
        DictData dictData = gateway.getByTypeAndValue(dictType, value);
        if(dictData != null){
            if(excludeId == null){
                throw new BizException(ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getCode(),
                        ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getMessage());
            } else if (!excludeId.equals(dictData.getId())) {
                throw new BizException(ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getCode(),
                        ErrorCodeConstants.DICT_TYPE_TYPE_DUPLICATE.getMessage());
            }
        }
    }

}
